package com.atguigu.eduorder.service.impl;

import com.atguigu.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.commonutils.ordervo.UcenterMemberOrderVo;
import com.atguigu.eduorder.entity.TOrder;
import com.atguigu.eduorder.feign.CourseApi;
import com.atguigu.eduorder.feign.UcenterApi;
import com.atguigu.eduorder.mapper.TOrderMapper;
import com.atguigu.eduorder.service.TOrderService;
import com.atguigu.eduorder.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Cyrus
 * @since 2020-12-18
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private CourseApi courseApi;

    @Autowired
    private UcenterApi ucenterApi;
    //生成订单方法
    @Override
    public String createOrder(String courseId, String userId) {
        //远程调用：获取用户信息
        CourseWebVoOrder courseInfo = courseApi.getCourseInfoById(courseId);
        //远程调用：课程信息
        UcenterMemberOrderVo UserInfo = ucenterApi.getMemberInfoById(userId);
        //设置订单值
        TOrder tOrder = new TOrder();
        tOrder.setOrderNo(OrderNoUtil.getOrderNo());
        tOrder.setCourseId(courseId);
        tOrder.setCourseTitle(courseInfo.getTitle());
        tOrder.setCourseCover(courseInfo.getCover());
        tOrder.setTeacherName(courseInfo.getTeacherName());
        tOrder.setTotalFee(courseInfo.getPrice());
        tOrder.setMemberId(userId);
        tOrder.setMobile(UserInfo.getMobile());
        tOrder.setNickname(UserInfo.getNickname());
        tOrder.setStatus(0);
        tOrder.setPayType(1);
        baseMapper.insert(tOrder);

        //返回订单号
        return tOrder.getOrderNo();


    }
}
